package ca.ualberta.cs.CLRServer.resources;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.xml.bind.JAXB;

import uk.ac.liv.c3connector.AnnotationModel;
import ca.ualberta.cs.BibTex.BibTex;
import ca.ualberta.cs.CLRDB.User;
import ca.ualberta.cs.CLRServer.Interface.SecureResource;
import ca.ualberta.cs.CLRServer.Interface.ServerInterface;

@Path("/publishService/resources")
public class ResourcesController extends SecureResource{

	
	@Path("/add")
	@POST 
    @Consumes("application/json")
    //@Produces("text/plain")
    public String addResource(List<JAXBBean> requestArr){
    	
		checkBufferOverflow(requestArr, 6); //when paperinfo is given, there are 6 fields
		checkRequiredKeys(requestArr, "url","username");
		
		boolean paperInfGiven = true;
		String bibtex = null;
		String url = null;
		String username = null;
		
		HashMap<String,String> paperInfo = new HashMap<String, String>();		
		
		for( JAXBBean bean : requestArr ){
		 	if(bean.getKey().equals("bibtex")){
		 		bibtex = bean.getVal();
		 		paperInfGiven = false;
		 	}
		 	else if(bean.getKey().equals("url"))
		 		url = bean.getVal();
		 	else if(bean.getKey().equals("username"))
		 		username = bean.getVal();
		 	else
		 		paperInfo.put(bean.getKey(), bean.getVal());
		}
    	 
		Integer resourceId = null;
		
		BibTex bib = null;
		if( bibtex != null)
			bib = new BibTex(bibtex);
		
		
		if(url.startsWith("file:/"))
			url = username + ":" + url;
		
			
		System.out.println("\n-----------------------------------------");
		System.out.println("a new resource is being added. URL: "+url);
		System.out.println("bibtex: "+bib);	
		System.out.println("-----------------------------------------\n");
		
//		resourceId = ServerInterface.db.getResourceManagement().addOrUpdateResource(bib, url);
		if(!paperInfGiven)
			resourceId = ServerInterface.db.resourceMgmt.getReadingManagement().pageAccessed(bib, null, url, username);
		else
			resourceId = ServerInterface.db.resourceMgmt.getReadingManagement().pageAccessed(null, paperInfo, url, username);
		
		return String.valueOf(resourceId);
    }
	
	@Path("/info/needed")
	@POST 
    @Consumes("text/plain")
    @Produces("text/plain")
    public String whatIsNeeded(String url){
		
		checkUrl(url);
    	
		return ServerInterface.db.getResourceManagement().getBibTexManagement().urlHasDoiAndKeywords(url, null);
    }
	
	@Path("/info/update")
	@POST 
    @Consumes("application/json")
    @Produces("text/plain")
    public String updateResourceBib(List<JAXBBean> requestArr){
		
		checkBufferOverflow(requestArr,3);
		checkRequiredKeys(requestArr, "url","doi","keywords");
		
    	String url ="";
    	String doi = null;
    	String keywords = null;
    	for(JAXBBean bean : requestArr){
    		if(bean.getKey().equals("url"))
    			url = bean.getVal();
    		else if(bean.getKey().equals("doi"))
    			doi = bean.getVal();
    		else if(bean.getKey().equals("keywords"))
    			keywords = bean.getVal();
    	}
    	
    	System.out.println("\n-----------------------------------------");
    	System.out.println("somebody is updating a resource. URL: "+url);
    	System.out.println("DOI: "+doi);
    	System.out.println("Keywords: "+keywords);
    	System.out.println("-----------------------------------------\n");
    	
		return String.valueOf(ServerInterface.db.getResourceManagement().getBibTexManagement().updateResourceBibtex(url, doi, keywords, null));
    }
		
}
